<template>
    <div id="add-edit-model">
        <el-dialog
            :title="titleCon"
            v-model="isUseDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="769px" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="100px" :rules="rules">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="课程类型" prop="V_CourseType">
                            <el-input v-model="ruleForm.V_CourseType" placeholder="请输入课程类型" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="计划培训时间">
                            <el-date-picker
                                v-model="ruleForm.V_PlanMonth"
                                type="date"
                                value-format="YYYY-MM-DD"
                                placeholder="请选择计划培训时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="课程名称" prop="V_CourseName">
                            <el-input v-model="ruleForm.V_CourseName" placeholder="请输入课程名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="培训讲师">
                            <el-input v-model="ruleForm.V_Teacher" placeholder="请输入培训讲师" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="培训内容" prop="V_Content">
                            <el-input v-model="ruleForm.V_Content" placeholder="请输入培训内容" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="培训对象" prop="V_Trainee">
                            <el-input v-model="ruleForm.V_Trainee" placeholder="请输入培训对象" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="培训时长">
                            <el-input v-model="ruleForm.N_Mins" placeholder="请输入培训时长" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="是否需要考试">
                            <el-switch
                                v-model="ruleForm.IsNeedExam"
                                active-color="#07AAF9"
                                inactive-color="#FF5F33"
                                inline-prompt
                                active-text="需要"
                                inactive-text="不需要"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">取 消</el-button>
                <el-button type="primary" :disabled="repeatDis" :loading="repeatDis" @click="submitForm(ruleFormRef)">提 交</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch } from 'vue'
import { FormInstance, FormRules , ElMessage, ElButton, ElCol, ElDatePicker, ElDialog, ElDivider, ElForm, ElFormItem, ElInput, ElOption, ElRadio, ElRow, ElSelect, ElTable, ElTableColumn } from 'element-plus'
import getTableData from '/@/utils/getTableData'
/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,
}
/*** form 表单数据类型 */
type ruleFormType = {
    n_Id?: Number | String,
    V_CourseType: String,
    V_PlanMonth: String,
    V_CourseName: String,
    V_Teacher: String,
    V_Content: String,
    V_Trainee: String,
    N_Mins: String,
    IsNeedExam: Boolean,
}
export default defineComponent( {
    name: 'add-edit-model',
    components: {},
    props: {
        isUseDialog:{
            type: Boolean,
            default: false,
        },
        repairInfo: {
            type: String,
            default: ''
        }
    },
    setup(props,ctx){
        const titleCon = ref()
        const ruleFormRef = ref()

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            // 课程类型
            V_CourseType: [
				{ required: true, message: '请输入课程类型', trigger: 'blur' },
			],
            // 课程类型
            V_CourseName: [
				{ required: true, message: '请输入课程名称', trigger: 'blur' },
			],
            // 培训内容
            V_Content: [
				{ required: true, message: '请输入培训内容', trigger: 'blur' },
			],
            // 培训对象
            V_Trainee: [
				{ required: true, message: '请输入培训对象', trigger: 'blur' },
			],
        })

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            ctx.emit('cancelUseClick')
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelUseClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    let param = toRaw(state.ruleForm)
                    console.log(param,'param')
                    return
                    state.repeatDis = true
                    ctx.emit('controlUseClick')
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** 监听isUseDialog的变化 */
        watch(() => props.isUseDialog,(newVal) => {
            if(newVal){
                if(props.repairInfo !== ''){
                    titleCon.value = '编辑'
                    // 状态数据重置
                    state.repeatDis = false;
                    let getData = JSON.parse(props.repairInfo);
                    state.ruleForm = getData;
                }else{
                    titleCon.value = '新增';
                    state.ruleForm = {
                        V_CourseType: '',
                        V_PlanMonth: '',
                        V_CourseName: '',
                        V_Teacher: '',
                        V_Content: '',
                        V_Trainee: '',
                        N_Mins: '',
                        IsNeedExam: true,
                    }
                }
            }
        })

        /*** isUseDialog 响应弹窗的关闭  */
        const isUseDialog = computed(() => {
            return props.isUseDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            isUseDialog,
            rules,
        }
    },
} )
</script>
<style scoped lang="scss">
    :deep(.el-select){
        width: 100%!important;
    }
    :deep(.el-input){
        width: 100%;
    }
</style>